package com.hiwhitley.himei.module.game;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.hiwhitley.himei.R;
import com.hiwhitley.himei.entity.GameVideoHorizontal;
import com.hiwhitley.himei.entity.ScreenItem;
import com.hiwhitley.himei.widget.glide.GlideImageView;
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;

import butterknife.BindView;
import butterknife.ButterKnife;
import me.drakeet.multitype.ItemViewProvider;

/**
 * Created by hiwhitley on 17-2-26.
 */
public class GameVideoHorizontalViewProvider
        extends ItemViewProvider<GameVideoHorizontal, GameVideoHorizontalViewProvider.ViewHolder> {


    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(
            @NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View root = inflater.inflate(R.layout.item_game_video_horizontal, parent, false);
        return new ViewHolder(root);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull GameVideoHorizontal gameVideoHorizontal) {
        prepareVideoView(holder, gameVideoHorizontal);
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.standard_video_player)
        StandardGSYVideoPlayer mStandardVideoPlayer;
        ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    private void prepareVideoView(ViewHolder holder, ScreenItem gameItem) {
        Context context = holder.mStandardVideoPlayer.getContext();
        StandardGSYVideoPlayer videoPlayer = holder.mStandardVideoPlayer;
        GlideImageView thumbImageView = new GlideImageView(context);
        thumbImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        thumbImageView.loadNetImage(gameItem.getVideo().getPre_image().getUrl(), R.drawable.bg_holder_brown);
        videoPlayer.setThumbImageView(thumbImageView);

        videoPlayer.setUp(gameItem.getVideo().getUrl(), false, gameItem.getGameName());
        //增加title
        videoPlayer.getTitleTextView().setVisibility(View.GONE);
        //设置返回键
        videoPlayer.getBackButton().setVisibility(View.GONE);
        videoPlayer.getFullscreenButton().setVisibility(View.GONE);
        //设置视频方向
        videoPlayer.setLockLand(gameItem.getItemType() == ScreenItem.HORIZONTAL_VIDEO);

        videoPlayer.getStartButton().setOnClickListener((v) -> {
                    videoPlayer.startPlayLogic();
                    videoPlayer.startWindowFullscreen(context, false, true);
                }
        );
    }
}